#include<bits/stdc++.h>
using namespace std;
typedef struct node{
	int data;
	struct node *lchild,*rchild;
}BTNode;
typedef struct node * BTree;
BTree CreatBT(int *s,int *t,int n);
void PreOrder( BTree BT );
int main(){
	BTree BT;
	BT=new BTNode;
	int n,i;
	cin>>n;
	int s[35],t[35];
	for(i=0;i<n;i++){
		cin>>s[i];
	}
	for(i=0;i<n;i++){
		cin>>t[i];
	}
	BT=CreatBT(s,t,n);
	cout<<"Preorder:";
	PreOrder(BT);
	cout<<endl; 
	return 0;
}
BTree CreatBT(int *s,int *t,int n){
 	BTree S;
	int *p;
	int k;
	if(n<=0) return NULL;
	S=new BTNode;
	S->data=s[n-1];
	for(p=t;p<t+n;p++){
		if(*p==s[n-1]) {
			break;
		}
	}
	k=p-t;
	S->lchild=CreatBT(s,t,k);
	S->rchild=CreatBT(s+k,t+k+1,n-k-1);
	return S;
}
void PreOrder( BTree BT ){
	if(BT){
		cout<<" "<<BT->data;
		PreOrder(BT->lchild);
		PreOrder(BT->rchild);
	}
}
